package com.idreems.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.idreems.item.Category;
import com.idreems.item.Item;
import com.idreems.mm.R;

public class MenuAdapter extends BaseAdapter
{

	public interface MenuListener
	{

		void onActiveViewChanged(View v);
	}

	private Context mContext;

	private List<Object> mItems;

	private MenuListener mListener;

	private int mActivePosition = -1;

	public MenuAdapter(Context context, List<Object> items)
	{
		mContext = context;
		mItems = items;
	}

	public void setListener(MenuListener listener)
	{
		mListener = listener;
	}

	public void setActivePosition(int activePosition)
	{
		mActivePosition = activePosition;
	}

	/**
	 * 得到第n个category下的项目
	 * 
	 * @param position
	 * @return
	 */
	public List<Item> getItems(int position)
	{
		Object obj = mItems.get(position);
		if (obj instanceof Item)
		{
			return ((Item)obj).subItems();
		}
		return new ArrayList<Item>();
//		if (mItems == null)
//		{
//			return new ArrayList<Item>();
//		}
//		// get data range
//		int head = 0;
//		int end = 0;
//		int current = 0;
//		for (int i = 0; i < mItems.size(); i++)
//		{
//			// find head and end
//			if (!(mItems.get(i) instanceof Item))
//			{
//				if (position != 0 && head == 0)
//				{
//					head = i;
//				}
//				end = i;
//				current++;
//			}
//
//			if (current > position)
//			{
//				break;
//			}
//		}
//
//		// get data
//		List<Item> itemList = new ArrayList<Item>();
//		for (int i = head; i < end; i++)
//		{
//			itemList.add((Item) mItems.get(i));
//		}
//		return itemList;
	}

	@Override
	public int getCount()
	{
		return mItems.size();
	}

	@Override
	public Object getItem(int position)
	{
		return mItems.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}

	@Override
	public int getItemViewType(int position)
	{
		return getItem(position) instanceof Item ? 0 : 1;
	}

	@Override
	public int getViewTypeCount()
	{
		return 2;
	}

	@Override
	public boolean isEnabled(int position)
	{
		return getItem(position) instanceof Item;
	}

	@Override
	public boolean areAllItemsEnabled()
	{
		return false;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		View v = convertView;
		Object item = getItem(position);

		if (item instanceof Category)
		{
			if (v == null)
			{
				v = LayoutInflater.from(mContext).inflate(R.layout.menu_row_category, parent, false);
			}

			((TextView) v).setText(((Category) item).mTitle);

		}
		else
		{
			if (v == null)
			{
				v = LayoutInflater.from(mContext).inflate(R.layout.menu_row_item, parent, false);
			}

			TextView tv = (TextView) v;
			tv.setText(((Item) item).mTitle);
			tv.setCompoundDrawablesWithIntrinsicBounds(((Item) item).mIconRes, 0, 0, 0);
		}

		v.setTag(R.id.mdActiveViewPosition, position);

		if (position == mActivePosition)
		{
			mListener.onActiveViewChanged(v);
		}

		return v;
	}
}
